package cn.com.huzo.service.inventory.impl;

import cn.com.huzo.model.entities.inventory.StockInLinesPoLinesEntity;
import huzofw.model.entities.paginator.Paginator;
import cn.com.huzo.repository.inventory.StockInLinesPoLinesRepository;
import huzofw.service.impl.GenericManagerImpl;
import cn.com.huzo.service.inventory.StockInLinesPoLinesManager;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.apache.commons.collections4.MapUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 入库单明细---引入采购订单明细数据管理服务类实现。
 *
 * @author 吴旭
 */
@Service("StockInLinesPoLinesManager")
public class StockInLinesPoLinesManagerImpl extends GenericManagerImpl<StockInLinesPoLinesEntity, String>
        implements StockInLinesPoLinesManager {

    @Resource(name = "StockInLinesPoLinesRepository")
    private StockInLinesPoLinesRepository _dao;

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Map<String, Object> fetchForPage(Map<String, Object> criterion) throws Exception {
        Map<String, Object> _result = new HashMap();

        Long offset = (long) (MapUtils.getInteger(criterion, "Limit") * (MapUtils.getInteger(criterion, "PageIndex") - 1));
        long total = _dao.fetchForCount(criterion);

        if (total > 0) {
            if (offset >= total) {
                offset = (total - MapUtils.getInteger(criterion, "Limit")) < 0 ? 0 : (total - MapUtils.getInteger(criterion, "Limit"));
            }
            criterion.put("Offset", offset);
            List<StockInLinesPoLinesEntity> itemList = _dao.fetchForPage(criterion);
            Paginator<StockInLinesPoLinesEntity> paginator = new Paginator<>(itemList, offset, MapUtils.getInteger(criterion, "Limit"), total);

            _result.put("TotalRecordCount", paginator.getTotal());
            _result.put("TotalPages", paginator.getTotalPages());
            _result.put("PageIndex", paginator.getPageIndex());
            _result.put("DataList", paginator.getItemList());
            _result.put("Success", true);
        } else {
            _result.put("Success", false);
        }

        return _result;
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Map<String, Object> update(List<StockInLinesPoLinesEntity> itemList, Integer limit02, Integer pageIndex02) throws Exception {
        Map<String, Object> _result = new HashMap();

        // 第一步，增加明细（引入的采购订单表头文件）
        Integer rowsAffrected = _dao.update(itemList);
        // 第二步，返回更新后的入库单明细（到货明细）
        if (rowsAffrected > 0) {
            Map<String, Object> criterion = new HashMap();
            criterion.put("HeaderId", itemList.get(0).getHeaderId());
            criterion.put("Limit", limit02);               // 每页显示行数
            criterion.put("PageIndex", pageIndex02);       // 当前为第一页
            _result.put("LinesPoLinesList", fetchForPage(criterion));
        }

        return _result;
    }
}
